/*
	   _           _      _           _     _ 
	  (_)         | |    | |         (_)   | |
	   _ _   _ ___| |_ __| |_ __ ___  _  __| |
	  | | | | / __| __/ _` | '__/ _ \| |/ _` |
	  | | |_| \__ \ || (_| | | | (_) | | (_| |
	  | |\__,_|___/\__\__,_|_|  \___/|_|\__,_|
	 _/ |                                     
	|__/ 
	
	Copyright 2013 Caspar Isemer and and Eva Krueger, http://justdroid.org
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
		http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License. 
*/

package org.justdroid.justdroid;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;


public class SwipingUtils extends FragmentActivity implements OnInitListener{
	
	protected View curtainView;
	protected boolean curtainSet = false;
	
	private TextToSpeech tts;
	
	MyPageAdapter pageAdapter;
	
	@Override
	public void onResume() {
		super.onResume();
		
		WindowManager windowManager = getWindowManager();
		LayoutInflater inflater = getLayoutInflater();
		LayoutParamsAndViewUtils layoutParamsAndView = ScreenCurtainFunctions.prepareForCurtainCheck(inflater);
    	
    	ScreenCurtainFunctions appState = ((ScreenCurtainFunctions) getApplicationContext());
    	if(appState.getState() && !curtainSet) {
    		curtainView = layoutParamsAndView.getView();
    		windowManager.addView(curtainView, layoutParamsAndView.getLayoutParams());
    		curtainSet = true;
    	} else if(!appState.getState() && curtainSet) {
    		windowManager.removeView(curtainView);
    		curtainSet = false;
    	}
	}
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.swiping);
        
        //check for TTS
        Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, 0);
              
        ScreenCurtainFunctions appState = ((ScreenCurtainFunctions)getApplicationContext());
        appState.setState(false);
        
		/** Set Roboto as the standard font **/
		if (Build.VERSION.SDK_INT < 11) {
		    ViewGroup godfatherView = (ViewGroup) this.getWindow().getDecorView();
		    FontUtils.setRobotoFont(this, godfatherView);
		}
        
        List<Fragment> fragments = getFragments();
        
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
        
        ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
        pager.setAdapter(pageAdapter);
        
    }
    
    private List<Fragment> getFragments(){
    	List<Fragment> fList = new ArrayList<Fragment>();

    	fList.add(new HomescreenActivity());
    	return fList;
    }
    
    private class MyPageAdapter extends FragmentPagerAdapter {
    	private List<Fragment> fragments;

        public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }
        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }
     
        @Override
        public int getCount() {
            return this.fragments.size();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == 0)
        {
        	if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS)
        	{
    	       //success
        		try
        		{
        			tts = new TextToSpeech(this, this);
        			TTS.setObject(tts);
        		}
        		catch(Exception e)
        		{
        			e.printStackTrace();
        		}
            }
        	else
        	{
        		//install the TTS data
        		Intent installIntent = new Intent();
        	 	installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        		startActivity(installIntent);
        	}
        }
    }

    
    public void onInit(int status)
    {
    	if(status == TextToSpeech.ERROR) {
    		Toast.makeText(getApplicationContext(), this.getResources().getString(R.string.ttsError), Toast.LENGTH_LONG).show();
    	}
    }
}
